package by.jtraining.london.repo.Impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import by.jtraining.london.model.Student;
import by.jtraining.london.repo.StudentRepository;

import com.google.inject.Inject;
import com.google.inject.persist.Transactional;

/**
 * @author Pakhomova Irina
 * 
 */

public class StudentRepositoryImpl extends GenericRepositoryImpl<Student, Long>
		implements StudentRepository {
	private static final Logger LOG = LoggerFactory
			.getLogger(StudentRepositoryImpl.class);

	@Inject
	public StudentRepositoryImpl(EntityManager entityManager) {
		super(entityManager, Student.class);
	}

	@Transactional
	public void add(Student student) {
		LOG.debug("Trying to persist {}", student);
		em.persist(student);
	}

	@Transactional
	public boolean isUploadApplicationFormForUser(long id) {//  (ID  id)
		LOG.debug("Trying to get istence application form for student");
		Student student = getById(id);
		if (student.getIsUploadApplicationForm()) {
			LOG.debug("Application Form for  student {} is exist", student);
			return true;
		} else {
			LOG.debug("Application Form for  student {} is not exist", student);
			return false;
		}
	}
	
	@Transactional
	public List<Student>  search(String queryTerm){
		String formQuery="SELECT st FROM Student AS st WHERE  "+queryTerm;
		TypedQuery<Student> query = em
				.createQuery(formQuery,	Student.class);
		List<Student> student = query.getResultList();
		return student;
	}
}
